<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
    <xsl:param name="DOTNET2"/>

  
	<xsl:template match="Class">
	<xsl:variable name="Name" select="Name"/>
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

using NUnit.Framework;

namespace <xsl:value-of select="//HeaderInfo/ProjectName"/>.Tests.DataAccess
{
	[TestFixture]
	public class <xsl:value-of select="Name"/>Test : NUnitBase
	{
	
		<![CDATA[/// <summary>]]>
		/// This NUnit test, creates a data object, populates it, ensures it is written to disk
		/// and checks the properties are correctly set.
		<![CDATA[/// </summary>]]>
		[Test]
		public void InsertDeleteRecord ( )
		{
			<xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Domain.DataAccess.<xsl:value-of select="Name"/> dataObject = new <xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Domain.DataAccess.<xsl:value-of select="Name"/> ( );
		
			<xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Interfaces.I<xsl:value-of select="Name"/>DataObject dalInterface = 
				new <xsl:value-of select="//HeaderInfo/ProjectName"/>.Data.NHibernateDal.DataObjectFactory ( ).Get<xsl:value-of select="Name"/>DataObject ( );
				
			int records = dalInterface.GetAll ( ).Count;
			
			// Fill the dataObject with data
			BuildRecord ( dataObject );
			
			dalInterface.Save ( dataObject );
			
			// NUnit test that the amount of <xsl:value-of select="Name"/> records has increased
			// in the database (confirms successful write)
			Assert.IsTrue ( records+1<xsl:for-each select="ClassAttribs/ClassAttributes"><xsl:if test="Type=$Name">+1</xsl:if></xsl:for-each> == dalInterface.GetAll ( ).Count );
			
			// Reset the NHibernate Session to force read from database.
			<xsl:value-of select="//HeaderInfo/ProjectName"/>.Data.NHibernateDal.SessionManager.Instance.CommitTransaction ( );
			<xsl:value-of select="//HeaderInfo/ProjectName"/>.Data.NHibernateDal.SessionManager.Instance.CloseSession ( );
			<xsl:value-of select="//HeaderInfo/ProjectName"/>.Data.NHibernateDal.SessionManager.Instance.BeginTransaction ( );
			
			// Reload the object by using the primary key
			dataObject = dalInterface.GetId ( dataObject.ObjectId );
			
			// Ensure the properties have retained their values
			PerformPropertyTest ( dataObject );
			
			// Delete all the test objects that we have created so as not to effect
			// the state of the database
			DeleteRecord ( dataObject , dalInterface );

			
		}
		
		
		<![CDATA[/// <summary>
		/// Ensures that all the properteis set in BuildRecord remain set when read from the database
		/// </summary>]]>
		private static void PerformPropertyTest ( <xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Domain.DataAccess.<xsl:value-of select="Name"/> obj )
		{
			// Ensure all properties are set to what was written 
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single' and KnownClass='false' and IsEnum='false']"><xsl:sort select="PropertyName" order="descending"/>
			Assert.IsTrue ( obj.<xsl:value-of select="PropertyName"/>.Equals ( NUnitBase.Test<xsl:value-of select="Type"/> ),
				string.Format ( CultureInfo.CurrentCulture , "{0} != {1}" , obj.<xsl:value-of select="PropertyName"/>, NUnitBase.Test<xsl:value-of select="Type"/> ) );</xsl:for-each>
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			Assert.IsTrue ( obj.<xsl:value-of select="PropertyName"/>.ObjectId != Guid.Empty ,
				string.Format ( CultureInfo.CurrentCulture , "{0} == {1}" , obj.<xsl:value-of select="PropertyName"/>.ObjectId, Guid.Empty ) );
			</xsl:for-each>
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='array' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			foreach ( <xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Domain.DataAccess.<xsl:value-of select="Type"/> arrayobject in obj.<xsl:value-of select="PropertyName"/> )
			{
				Assert.IsTrue ( arrayobject.ObjectId != Guid.Empty ,
				string.Format ( CultureInfo.CurrentCulture , "{0} == {1}" , arrayobject.ObjectId, Guid.Empty ) );
			}
			</xsl:for-each>
		}
		
		
		
		<![CDATA[/// <summary>
		/// Delete the record and all related objects
		/// </summary>]]>		
		private static void DeleteRecord ( <xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Domain.DataAccess.<xsl:value-of select="Name"/> obj , 
			<xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Interfaces.I<xsl:value-of select="Name"/>DataObject dalInterface )
		{
			// Foreign key restraints mean we have to delete the main object before
			// removing any of the linked objects ( if any )
			dalInterface.Delete ( obj );
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			new <xsl:value-of select="//HeaderInfo/ProjectName"/>.Data.NHibernateDal.DataObjectFactory ( ).Get<xsl:value-of select="Type"/>DataObject ( ).Delete ( obj.<xsl:value-of select="PropertyName"/> );
			</xsl:for-each>
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='array' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			foreach ( <xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Domain.DataAccess.<xsl:value-of select="Type"/> arrayObject in obj.<xsl:value-of select="PropertyName"/> )
			{
				new <xsl:value-of select="//HeaderInfo/ProjectName"/>.Data.NHibernateDal.DataObjectFactory ( ).Get<xsl:value-of select="Type"/>DataObject ( ).Delete ( arrayObject );
			}
			</xsl:for-each>
		}
		
		
		<![CDATA[/// <summary>
		/// Build a test record which will be used for writing and reading from the database
		/// </summary>]]>
		private static void BuildRecord ( <xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Domain.DataAccess.<xsl:value-of select="Name"/> obj )
		{
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single' and KnownClass='false' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			obj.<xsl:value-of select="PropertyName"/> = NUnitBase.Test<xsl:value-of select="Type"/>;</xsl:for-each>
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			obj.<xsl:value-of select="PropertyName"/> = new <xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Domain.DataAccess.<xsl:value-of select="Type"/> ( );
			new <xsl:value-of select="//HeaderInfo/ProjectName"/>.Data.NHibernateDal.DataObjectFactory ( ).Get<xsl:value-of select="Type"/>DataObject ( ).Save ( obj.<xsl:value-of select="PropertyName"/> );
			</xsl:for-each>
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='array' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			<xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Domain.DataAccess.<xsl:value-of select="Type"/> _<xsl:value-of select="Name"/>
				= new <xsl:value-of select="//HeaderInfo/ProjectName"/>.Core.Domain.DataAccess.<xsl:value-of select="Type"/> ();
			new <xsl:value-of select="//HeaderInfo/ProjectName"/>.Data.NHibernateDal.DataObjectFactory ( ).Get<xsl:value-of select="Type"/>DataObject ( ).Save ( _<xsl:value-of select="Name"/> );
			obj.<xsl:value-of select="PropertyName"/>.Add ( _<xsl:value-of select="Name"/> );
			</xsl:for-each>
		
		}
	
	}
	

}
	
	</xsl:template>
</xsl:stylesheet>